import Vue from 'vue'
import App from './App.vue'
import Vuex from 'vuex'

Vue.use(Vuex)
const store = new Vuex.Store({
  state: {
    all: false,
    goodList: [
      {
        name: '诸葛亮',
        price: 1000,
        num: 1,
        checked: false,
      },
      {
        name: '蔡文姬',
        price: 1500,
        num: 1,
        checked: false,
      },
      {
        name: '妲己',
        price: 2000,
        num: 1,
        checked: false,
      },
      {
        name: '鲁班',
        price: 2200,
        num: 1,
        checked: false,
      },
    ],
  },
  mutations: {
    del(state, index) {
      if (state.goodList[index].num > 1) {
        state.goodList[index].num--
      }
    },
    add(state, index) {
      state.goodList[index].num++
    },
    splicList(state, index) {
      state.goodList.splice(index, 1)
    },
    checkAll(state,all){
      state.all=all
      state.goodList.forEach(item=>item.checked=all)
    },
    AllCheck(state){
      // console.log(state.all);
      return state.all= state.goodList.every(item=>item.checked)
     
    },
  },
  getters: {
    goodList: (state) => state.goodList,
    totalPrice(state) {
      return state.goodList.reduce((acc, item) => {
        if (item.checked) {
          acc += item.price * item.num
        }
        return acc
      }, 0)
    },
    // AllCheck(state){
    //   // console.log(state.all);
    //   return state.all= state.goodList.every(item=>item.checked)
     
    // },
  },
})
Vue.config.productionTip = false

new Vue({
  store,
  render: (h) => h(App),
}).$mount('#app')
